import Dao
import User

class UserProcessor:

	def __init__(self):
		""" default instructor """
		self._dao = Dao.Dao()
		self._userDictionary = self._dao.getUserDictionary()

		
	def addUser(self, userName, email):
		""" add a user to the userlist """
		if self.userExists(userName)=='-1':
			if self.emailExists(email)=='-1':
				datalist=[]
				datalist.append(userName)
				datalist.append(email)
				self._dao.addUserToDictionary(datalist)
				return True
		return False
			
	def addBookToUser(self, userid, bookid):
		""" add the book the the user book list """
		user = self._userDictionary[userid]
		return user.addBookToList(bookid)
		
	def delBookfromUser(self, bookid):		
		""" remove the book from the user book list """
		for user in self._userDictionary.values():
			if user.removeBookFromList(bookid):
				return True
		return False

		
	def getUserDictionary(self):
		return self._userDictionary
		
	def getUser(self, userid):
		return self._userDictionary[userid]
		
	def userExists(self, username):
		""" return the userid if it exists. """
		#print 'looking for' + username
		for userid in self._userDictionary.keys():
			#print self._userDictionary[userid].getUsername()
			if self._userDictionary[userid].getUsername() == username:
				#print 'found user name'
				return userid
			    
		return '-1'
	
	def emailExists(self, email):
		""" return the email if it exists. """
		for userid in self._userDictionary.keys():
			if self._userDictionary[userid].getEmail() == email:
				return userid
		return '-1'